﻿// 功能：静态地图展示窗口
// 描述：
// 作者：温伟鹏
// 日期：2010/10/22 16:55:02

using System;
using System.Collections.Generic;
using System.Text;
using GoogleMaps.StaticMap;
using MeizuSDK.Presentation;
using GoogleMaps;
using System.Drawing;
using System.Threading;

namespace Demo
{
    class StaticMapWindow : DemoWindowBase
    {
        MapView mapView;

        ScrollableControl scrollableControl;

        TrackBar tbZoom;
        Label lblZoomLabel;
        TextBox txtLongitude;
        TextBox txtLatitude;
        Button btnRefresh;

        public StaticMapWindow()
        {
            scrollableControl = new ScrollableControl();
            scrollableControl.Location = new System.Drawing.Point(0, 0);
            scrollableControl.Size = new System.Drawing.Size(Width, Height - ToolBar.HEIGHT - 2);
            Controls.Add(scrollableControl);

            mapView = new MapView();
            mapView.Location = new System.Drawing.Point(0, 0);
            mapView.Size = new System.Drawing.Size(480, 600);
            mapView.MapType = MapType.Hybrid;

            scrollableControl.Controls.Add(mapView);

            tbZoom = new TrackBar();
            tbZoom.Height = 50;
            tbZoom.Width = 300;
            tbZoom.Max = 21;
            tbZoom.Min = 0;
            tbZoom.Value = 5;
            tbZoom.Location = new System.Drawing.Point(0, 610);
            tbZoom.ValueChanged += new EventHandler(tbZoom_ValueChanged);

            scrollableControl.Controls.Add(tbZoom);

            lblZoomLabel = new Label();
            lblZoomLabel.Size = new Size(100, 50);
            lblZoomLabel.Location = new Point(310, 610);
            lblZoomLabel.Text = tbZoom.Value.ToString();

            scrollableControl.Controls.Add(lblZoomLabel);

            txtLongitude = new TextBox();
            txtLongitude.Size = new System.Drawing.Size(200, 100);
            txtLongitude.Location = new System.Drawing.Point(10, 680);
            txtLongitude.Tip = "经度：";
            txtLongitude.Text = "40.711614";

            scrollableControl.Controls.Add(txtLongitude);

            txtLatitude = new TextBox();
            txtLatitude.Size = new System.Drawing.Size(200, 100);
            txtLatitude.Location = new System.Drawing.Point(240, 680);
            txtLatitude.Tip = "纬度：";
            txtLatitude.Text = "-74.012318";

            scrollableControl.Controls.Add(txtLatitude);

            btnRefresh = new Button();
            btnRefresh.Text = "刷新地图";
            btnRefresh.ButtonType = Button.Type.Default;
            btnRefresh.Size = new System.Drawing.Size(200, 100);
            btnRefresh.Location = new System.Drawing.Point(240, 800);
            btnRefresh.Click += new EventHandler(btnRefresh_Click);

            scrollableControl.Controls.Add(btnRefresh);
        }

        void btnRefresh_Click(object sender, EventArgs e)
        {
            mapView.Center = new GoogleMaps.GeoPoint(Convert.ToDouble(txtLongitude.Text), Convert.ToDouble(txtLatitude.Text));
            Marker marker = new Marker(Color.Red, "A");
            marker.AddPoint(mapView.Center);
            mapView.AddMarker(marker);

            mapView.LoadMap();
        }

        void tbZoom_ValueChanged(object sender, EventArgs e)
        {
            lblZoomLabel.Text = tbZoom.Value.ToString();

            ThreadPool.QueueUserWorkItem(new WaitCallback(RefreshMap));
        }

        private void RefreshMap(object stateInfo)
        {
            try
            {
                mapView.ZoomLevel = (byte)tbZoom.Value;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, ex.Message, "错误");
            }
        }
    }
}
